﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Security;

namespace ZYLeGo2._0.Models
{
    public class MyAuthentication
    {
        /// <summary>
        /// 获取登录凭据
        /// </summary>
        /// <returns></returns>
        public static HttpCookie AuthCookie()
        {
            return HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
        }
        /// <summary>
        /// 设置用户登录成功凭据（cookie存储）
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="UserID">用户编号</param>
        /// <param name="Rights">权限</param>
        public static void SetCookie(string UserName,string UserID,string Rights)
        {
            String UserData = UserID + "#" + Rights;
            //数据放入ticket
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(60), false, UserData);
            //加密
            string enyTicket = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 判断用户是否登录
        /// </summary>
        /// <returns></returns>
        public static bool IsLogin()
        {
            return AuthCookie() != null;
        }
        /// <summary>
        /// 注销登录
        /// </summary>
        public static void LogOut()
        {
            FormsAuthentication.SignOut();
        }
        /// <summary>
        /// 获取凭据中的用户名
        /// </summary>
        /// <returns>用户名</returns>
        public static string GetUserName()
        {
            if (IsLogin())
            {
                //解密
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(AuthCookie().Value);
                return authTicket.Name;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 获取凭据中的用户ID
        /// </summary>
        /// <returns>用户ID</returns>
        public static int GetUserId()
        {
            if (IsLogin())
            {
                //解密
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(AuthCookie().Value);
                //拆分用户数据
                string[] UserData = authTicket.UserData.Split('#');
                if (UserData.Length>0)
                {
                    return int.Parse(UserData[0]);
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取用户凭据中的用户权限
        /// </summary>
        /// <returns>用户权限</returns>
        public static string GetRights()
        {
            if (IsLogin())
            {
                //解密
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(AuthCookie().Value);
                //拆分用户数据
                string[] UserData = authTicket.UserData.Split('#');
                if (UserData.Length > 1)
                {
                    return UserData[1].ToString();
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 根据登录用户ID获取会员实体类
        /// </summary>
        /// <returns></returns>
        public static User GetUser()
        {
            int uid = GetUserId();
            using (TradeDBEntities1 db = new TradeDBEntities1())
            {
                return db.Users.Find(uid);
            }       
        }
    }
}